﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers; 
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
namespace ITXHWEB.Controllers
{
    public partial class VKPOSController : ITXController
    {
        VVKPOS VVKP;
        VVKKOPF VKK;
        public List<VKPOSModel> _VKPS;
        public List<VKPOSModel> VKPS
        {
            get
            {
                return _VKPS;
            }
            set
            {
                _VKPS = value;
            }
        }
        public string LIEFSCH { get; set; }

        public string VKPAUF { get; set; }
        public bool IsVKPReady
        {

            get;
            set;
        }
        public VKPOSController()
        {
            VVKP = new VVKPOS();
            VKK = new VVKKOPF(); 
             
        }
        [ChildActionOnly]
        public ActionResult SCANVKPOS(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            VKPOSModel vkp = new VKPOSModel(); 
            return PartialView(vkp);
        }
        [ChildActionOnly]
        public ActionResult MVKPOS(string returnUrl)
        {
            ManageMessageId message = ManageMessageId.RemoveLoginSuccess;
            List<VKPOSModel> MyVKPS = null;
            if (TempData.ContainsKey("message"))
            {
                message = (ManageMessageId)TempData["message"];
            }
            if (TempData.ContainsKey("message"))
            {
                MyVKPS = (List<VKPOSModel>)TempData["VKPS"];
            }
            ViewBag.VKPS = MyVKPS;
            ViewBag.ReturnUrl = returnUrl;
            return PartialView();
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MVKPOS(VKPOSModel model, string returnUrl)
        {

            VKPAUF = model.VKPAUF;
            LIEFSCH = model.LIEFSCH; 
            ViewBag.IsVKPReady = true;
            ManageMessageId? message = null;
            message = ManageMessageId.SetPasswordSuccess;
            return RedirectToAction("ITXWB06", "ITXWB06", new { Message = message });
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SCANVKPOS(VKPOSModel model, string returnUrl)
        {

            VKPAUF = model.VKPAUF;
            LIEFSCH = model.LIEFSCH;
            CheckVV(model);
            ViewBag.IsVKPReady = IsVKPReady;
            ManageMessageId? message = null;
            message = ManageMessageId.SetPasswordSuccess;
            TempData["message"] = message;
            TempData["VKPS"] = VKPS;
            return RedirectToAction("ITXWB06", "ITXWB06", new { TempData = TempData });
            //return View(model);
        }
        public void CheckVV(VKPOSModel model)
        {
            IsVKPReady = true;
            if (model.VKPAUF.Length < 8)
                IsVKPReady = false;
            if (model.LIEFSCH.Length < 5)
                IsVKPReady = false;
            DataSet SReult = VVKP.VVVKP(VKPAUF.Substring(0, 2), VKPAUF.Substring(2, 6), LIEFSCH);
            _VKPS = new List<VKPOSModel>();
            foreach (DataRow row in SReult.Tables[0].Rows)
            {
                VKPOSModel vkp = new VKPOSModel();
                vkp.LIEFSCH = LIEFSCH;
                vkp.VKPAUF = row["VKPAUFKRS"].ToString() + row["VKPAUFLFD"].ToString() + row["VKPAUFPOS"].ToString();
                vkp.VFART = row["VKPKOLL"].ToString() + row["VKPART"].ToString() + row["VKPFARBE"].ToString();
                _VKPS.Add(vkp);
                IsVKPReady = true;
            }
        } 
    }
}
